
<%@page import="org.biojava.dasobert.das.CapabilityStatus"%><jsp:directive.page session="true" />

<%@ page import="org.biojava.dasobert.dasregistry.*"%>

<jsp:useBean id="register" class="dasregistry.registryBean"	scope="page" />
<jsp:setProperty name="register" property="*" />

<jsp:directive.page import="dasregistry.dasClientLink" />
<jsp:directive.page	import="org.biojava.dasobert.dasregistry.DasSourceComparator" />
<jsp:directive.page import="org.biojava.dasobert.dasregistry.DasSource" />
<jsp:directive.page import="org.biojava.services.das.registry.DasClient" />
<jsp:directive.page import="org.biojava.services.das.registry.DasRegistrySql" />
<jsp:directive.page import="org.biojava.services.das.registry.AutoActivation" />
<jsp:directive.page import="dasregistry.ActivationURLCreator" />
<jsp:directive.page import="java.util.Date" />
<jsp:directive.page import="java.util.Map" />
<jsp:directive.page import="java.util.List" />
<jsp:directive.page import="java.util.Comparator" />
<jsp:directive.page import="java.text.SimpleDateFormat" />
<%@ page import="org.biojava.dasobert.das.Capabilities"%>
<%@ page import="org.biojava.services.das.dao.Role" %>
<%@ page import="org.biojava.services.das.dao.RegistryUser" %>
<%@ page import="org.biojava.services.das.registry.UserManager" %>
<%@ page import="org.biojava.services.das.dao.ProjectBean"%>

<%@page import="org.biojava.services.das.registry.ShowLinkCapability"%>
<%@page import="org.biojava.services.das.registry.CapabilityLinkBean"%>
<jsp:directive.page import="org.biojava.dasobert.dasregistry.DasSource" />
<%@ page import="org.biojava.dasobert.das.Capabilities"%>
<%@page import="java.util.HashMap"%>
<jsp:directive.page import="org.springframework.beans.factory.BeanFactory"/>
<jsp:directive.page import="org.springframework.context.support.ClassPathXmlApplicationContext"/>
<jsp:directive.page import="org.biojava.dasobert.dasregistry.RegistryTimer"/>


<%
	// init
	response.setHeader("Cache-Control","private"); //HTTP 1.1
response.setHeader("Pragma","private"); //HTTP 1.0
response.setDateHeader ("Expires", -1); //prevents caching at the proxy server


	String findAction = request.getParameter("cmd");
	String sort       = request.getParameter("sort");
	String reverse    = request.getParameter("reverse");

	DasClient[] clients = ActivationURLCreator.CONFIG_DAS_CLIENTS;
	ActivationURLCreator activCreator = new ActivationURLCreator();

	BeanFactory ctx=(new ClassPathXmlApplicationContext("SpringDAO.xml"));
		DasRegistrySql registry=(DasRegistrySql)ctx.getBean("registryBean");

	int DEFAULT_STEPSIZE = 10;
%>


<%
	String title = "Available DAS sources";
	String header = title;
	
	if ( (findAction != null) && (findAction.equals("showmine"))) {
		title = "My DAS sources";
		header = title;
		
	}
%>

<%@ include file="pagehead.jsp" %>
    
<%
    	if ( email != null ) {
    %><script type="text/javascript">
<!--	
function checkUncheckAll(flag) {
     var theForm = document.getElementById('dassourceaction');
     var z = 0;
	 for(z=0; z<theForm.length;z++){
      	if(theForm[z].type == 'checkbox' ){
	  		theForm[z].checked = flag;
	  	}
	 }
}

function submitAction(object){
	var pos = object.options[object.selectedIndex].value;
	var theForm = document.getElementById('dassourceaction');
	
	if ( pos != 0 ) {
	
		if (pos == 'delete'){
			var ok = confirmation();
			if ( ok )
				theForm.submit();
		} else {
			theForm.submit();
		}
	}
}

function confirmation() {
	var answer = confirm("Are you sure you want to delete this DAS source?")
	if (answer){
		return true;	
	}
	else{
		return false;
	}
}

//--></script>
<%
	}
%>
    
<jsp:include page="openwindowscript.jsp" />
    
<body onload="initDynamicOptionLists();">
<%@ include file="pagebodystart.jsp" %>


<form method="post" action="listServices.jsp" name="count_form">	
 <input type="text" name="keyword" size="10" value=""/>
<input type="hidden" name="cmd" value="keyword"/>
  <input type="submit" value="search"/> 
</form>
<form method="post" action="listServices.jsp" name="count_form">
<input type="text" name="keywordForTypes" size="10" value=""/> 
   <input type="hidden" name="cmd" value="keywordForTypes"/>
  <input type="submit" value="search types only">
These searches use a crude database search mechanism so are slower and less powerful than using the search box in the top right hand corner. However they are constantly up to date whereas the search box on the top is only updated every 24 hrs.
</form>

<div id="normalcontent">
<div class="contentarea">

<%
	String message= (String)request.getAttribute("message");
	if ( message != null ){
%><p><strong><%=message%></strong></p><%
	}
%>

<h2>available DAS services</h2>
<p>
<%
	// include file="keywordsearch.jsp"
%> <!-- the search menu --> 

<%
 	String keyword = "";
 	String keywordForTypes="";

  	if ((findAction != null) && (findAction.equals("keyword"))) {
  		keyword = request.getParameter("keyword");
  		register.setCSTypes(new String[0]);
  		register.setCapabilities(new String[0]);
  		register.setLabels(new String[0]);
  		register.setOrganism("");
 %>filter by: <b><%=keyword%></b>
<%
	}else if ((findAction != null) && (findAction.equals("keywordForTypes"))) {
 		keywordForTypes = request.getParameter("keywordForTypes");
 		register.setCSTypes(new String[0]);
 		register.setCapabilities(new String[0]);
 		register.setLabels(new String[0]);
 		register.setOrganism("");
%>filter by types with: <b><%=keywordForTypes%></b>
<%
	} else if ((findAction != null) && ( findAction.equals("showmine"))) {
%>DAS sources for user <b><%=email%></b>		
		<%
					}
				%> 

<%@ include	file="selectservertable.jsp"%> 

<!-- end of menu-->


<%-- prepare the data structures --%>
<%@ include file="listServicesPrepare.jsp" %>


<%@ include file="sangertablestart.jsp"%> 


<%
 	String posargs = paramstring + sortstring;

  	if (reverse != null) {
  		posargs += notreversestring;
  	}

  	int maxpos = nrServers - stepsize;
 %>
<%@ include file="listbrowser.jsp"%>

<%@ include file="sangertablestart.jsp"%> 







<%
 	// if logged in the user can do more with the DAS sources
 if ( email != null ) {
 %>
	
	<form id="dassourceaction" name="dassourceaction" action="DasSourceAction" method="POST" >
	
	<tr class="1">
	
	<td colspan="6">Select: <a href="#" onClick="checkUncheckAll(true);">All</a>
	<a href="#" onClick="checkUncheckAll(false);">None</a>
	</td>
	
	<td align="right" colspan="3">action
		<select name="action" onChange="submitAction(this.form.action);">
		<option value="">select ...</option>
		<option value="validate">   validate</option>

		<%
			if ( (findAction != null) && (findAction.equals("showmine"))){
		%>
		<option value="add">   add to project</option>
		<option value="share">   share ownership</option>
		<option value="unown">   decline ownership</option>
		<option value="delete">   delete</option>

		<%
			} else {
		%>
		<option value="claim">   claim ownership</option>
		<%
			}
		%>
		</select>
	</td>
	</tr>
	
	<tr class="row1">
<%
	} else {
%>
<tr class="row0">
<%
	}
%>


	<td><b>pos</pos></td>
	<td><b><a href="listServices.jsp?sort=id<%=paramstring%><%=reversestring%>">id</a></b></td>

	<td><b>clients</b></td>

	<td><b>
		<a href="listServices.jsp?sort=nickname<%=paramstring%><%=reversestring%>">nickname</a>
	</b></td>

	<td><b>
		<a href="listServices.jsp?sort=status<%=paramstring%><%=reversestring%>">status</a>
	</b></td>

	<td><b>
		<a href="listServices.jsp?sort=capabilities<%=paramstring%><%=reversestring%>">capabilities</a>
	</b></td>
<td><b>types
	</b></td>
	<td><b>
		<a href="listServices.jsp?sort=coordinatesystem<%=paramstring%><%=reversestring%>">coordinateSystem</a>
	</b></td>

	<td><b>
		<a href="listServices.jsp?sort=description<%=paramstring%><%=reversestring%>">description</a>
	</b></td>
	
	<td><a href="help_specification.jsp">Specification</a></td><td><a href="listProjects.jsp">project</a></td>

</tr>



<%
	int i = 0;
	int numberservers = 0;
	if (s != null) {
		numberservers = s.size();
	}

	int twodays = 1000 * 60 * 60 * 24 * 2;
	int warningOfDeletionTime= 1000 * 60 * 60 * 24 * DasRegistrySql.ARCHIVE_TIME /2;//to show row as red as immenently going to be deleteds
	Date now = new Date();

	for (i = 0; i < numberservers; i++) {

		if ((i < pos) || (i > (pos + stepsize - 1)))
	continue;

		DasSource ds = s.get(i);

		// color column

		
		boolean active = true;
		boolean scheduledForDeletion=false;
		if (ds.getLeaseDate().getTime() < (now.getTime() - twodays)){
	active=false;
		}
		RegistryTimer timer=new RegistryTimer();
		int daysBeforeArchiving=timer.daysBeforeArchiving(ds);
		if ( daysBeforeArchiving< (DasRegistrySql.ARCHIVE_TIME/2)) {
	scheduledForDeletion=true;
	//the server has not had any valid capabilities for 2 days and will be scheduled for archiving if not valid at all for 30 days
	
	if (i % 2 != 0) {
%>
<tr class="invalid row1" valign="top">
	<%
	} else {
	%>

<tr class="invalid row2" valign="top">
	<%
			}
			} else {
				if (i % 2 != 0) {
	%>

<tr class="row1" valign="top">
	<%
	} else {
	%>

<tr class="row0" valign="top">
	<%
			}
			}

			// init some variables
			List<String> capabs = ds.getCapabilities();
			//String [] validCapabs=ds.getValidCapabilities();
			//for(int vCaps=0;vCaps<validCapabs.length;vCaps++){
			//System.out.println("valid cap ="+validCapabs[vCaps]);
			//}
			DasCoordinateSystem[] coordSys = ds.getCoordinateSystem();

			String testCode = "";

			boolean canFeatures = false;
			if (coordSys.length > 0) {
				testCode = coordSys[0].getTestCode();
				for (int j = 0; j < capabs.size(); j++) {
			String capability = capabs.get(j);
			if (capability.equals("features"))
				canFeatures = true;
				}
			}

			String url = ds.getUrl();
			String nickname = ds.getNickname();
		
	%>

	<td nowrap valign="top" ><% if (email != null ) { 
		%><input type="checkbox" name="DS_ID" value="<%=ds.getId() %>"/><%
	}
	%><%=i + 1%><% 
		if ( coord.userOwnsASource(user,new DasSource[]{ds})) {
			%>
			<div title="header=[owner] body=[I am owner of this DAS source]"><img src="img/kuser.png"/></div>
			<div title="header=[edit] body=[edit the registration information]"><a href="editServer.jsp?auto_id=<%=ds.getId() %>"><img src="img/pencil16x16.png" alt="edit"/></a></div>				
			<% 
		}
	%></td>
	<td nowrap><div title="header=[description] body=[<%=ds.getDescription()%>]" ><%=ds.getId()%></div>
	
	<div id="dassourceactions" title="header=[show details] body=[<%=ds.getDescription() %> ]"><a href="showdetails.jsp?auto_id=<%=ds.getId()%>"> 
				<img src="img/messagebox_info16x16.png" border="0" alt="details"
				onMouseOver="window.status='show details'; return true;"
				onMouseOut="window.status=' '; return true;"/>
				</a></div>			
			<div id="dassourceactions" title="header=[validate] body=[validate the <%=ds.getNickname()%> server]"><a href="validateServer.jsp?auto_id=<%=ds.getId()%>"> 
				<img 	src="img/ok16x16.png" border="0" alt="validate"
				onMouseOver="window.status='validate'; return true;"
				onMouseOut="window.status=' '; return true;"></a>
				</div>
			<div id="dassourceactions" title="header=[send to friend] body=[notify a friend about the <%=ds.getNickname()%> server]">
				<a href="sendToFriend.jsp?auto_id=<%=ds.getId()%>"> 
				<img src="img/mail_post_to16x16.png" boder="0" alt="edit"
				onMouseOver="window.status='send to friend'; return true;"
				onMouseOut="window.status=' '; return true;"></a>
				</div>
		

	</td>

	<%
	// the clients icons
	%>
	<td>
	<div id="clienticons">
	<!--  icons here --> <%
						if (canFeatures && active) {

						AutoActivation[] aas = activCreator.getAllActivationURLs(ds
						.getUrl());

						for (int l = 0; l < aas.length; l++) {
					AutoActivation aa = aas[l];
					DasClient client = aa.getClient();
			%>
			<div
				title="header=[activate in das client] body=[folow this link to activate this DAS source in the DAS client <%=client.getName() %>]">
			<a href="<%=aa.getURL(ds)%>"> <%
								if (client.getIconPath() != null) {
								out.print("<img src=\"" + client.getIconPath()
								+ "\" border=\"0\"/>");
							} else {
								out.print(client.getName());
							}
					%> </a></div>
			<%
 							}
 							}

 							
 					%>
		</div>
	</td>

	<td>
	<%
				if (!active) 
				out.print("<i>");
	%>
	<div title="header=[description] body=[<%=ds.getDescription()%>]"><%=ds.getNickname()%></div>
	<%
 			if (!active)
 			out.print("</i>");
				
 %>
	</td>

	<td>
	<%
	if (!active) {
	%> <div title="header=[server down?] body=[it was not possible to get any valid response from this server in > 2 days, deletion in <%=daysBeforeArchiving %> days]"><a href="validateServer.jsp?auto_id=<%=ds.getId()%>"><img
		src="img/messagebox_warning.png"></a></div> <%
 			} else {

 			if ( url.length() > 7 )	
	 			url = url.substring(7, url.length());
 			if (url.length() > 40) {
 		url = (url.substring(0, 15) + " ... " + url.substring(
 				(url.length() - 15), url.length()));
 			}
 			//out.print(url);
 		}
	if(scheduledForDeletion){
 %>Days before deletion:
 <%=daysBeforeArchiving %>
 <%}%>
	</td>
	<td nowrap>
	
			<%

ValidationResultLights lights=new ValidationResultLights(ds);
			List<LightBean> beans=lights.getLightsLinksAndResults();	

	//loop over all possible capabilities for a das server in the order we want as specified by the getCapabilitiesInCoreOrder
	for(LightBean linkBean:beans ){
		
		
		
			%>
			<span title="<%=linkBean.getCapability().toString()%><%=linkBean.getMessageHeader()%><%=linkBean.getCapability().toString()%> <%=linkBean.getMessageBody()%>">
			<a href="javascript:openWindow('<%=linkBean.getCmd()%>','',700,700)"><img src=<%=linkBean.getImg()%>></a> </span>
			<%
		
		
	}//end of looping over all capabilities
	
	
	%>
	</td>
	
	<td>
	<%String id="";
	CapabilityStatus statusOfTypes=lights.getStatus(Capabilities.TYPES);
	if(statusOfTypes==CapabilityStatus.VALID || statusOfTypes==CapabilityStatus.PROBABLY_VALID){
	//if types is stated then set the id to auto_id so we do lazy loading via jQuery (custom.js)
	//System.out.println(ds.toString());
		if(ds.getId()!=null){
			int auto_id=registry.getAutoID4DasSource(ds);
			//System.out.println("id in listservices"+ds.getId()+"auto_id="+auto_id);
			%>
			<a class="click">Expand/Contract</a>
			<div class="ajaxTypes" id="<%=auto_id %>"></div>
					
			<%
		}
	}
	%></td>
	<td>
	<%
	
				for (int j = 0; j < coordSys.length; j++) {
				DasCoordinateSystem cs = coordSys[j];
	%><a href="coordsys/<%=cs.getUniqueId() %>"><%=cs.toString()%></a>
					<%
					}
					%>
	</td>

	<td>
	
	<%
				// make compact and expandable display of description;

				int displength = 0;
				int longlength = 0;

				String desc = ds.getDescription();

				if (desc.length() > longlength) {
		%><%=desc.substring(0, displength)%><span id="rcc-<%=i%>"><a href="#rev" onclick="document.getElementById('rcc-<%=i%>').style.display = 'none'; document.getElementById('rce-<%=i%>').style.display = 'inline'; return false;">show&nbsp;&gt;</a></span><span id="rce-<%=i%>" style="display: none"><%=desc.substring(displength, desc.length())%></span>
		<%
		} else {
		%>
		<%=desc%>
		<%
		}
		%>
	
	<%
				String helperurl = ds.getHelperurl();
				if ((helperurl != null) && (!helperurl.equals(""))) {
		%><div title="header=[get more information] body=[show me more information about this DAS source in another window]"> 
			<a href="javascript:openWindow('<%=helperurl%>','',640,480)">go&nbsp;to&nbsp;site</a>
			</div> 
			<%
 			}
 			%>
	</td>
	<td><%=lights.getSpecification()%></td>
	<td>
	<%
	ProjectBean tp = coord.getProjectBeanForURL(ds.getUrl());
	if (tp != null) {
		%>
		<div title="header=[<%= tp.getName() %>] body=[<%=tp.getDescription() %>]">
		<a href="showProjectDetails.jsp?project_id=<%=tp.getProjectID() %>"><img src="<%= tp.getIconURL()%>"/></a></div><%
	}
%>
	
	</td>
	
	</tr>

	<%
	}
	%>


<% 
// if logged in the user can do more with the DAS sources
if ( email != null ) {
	
	%></form><% 	
}
%>



<%@ include file="listbrowser.jsp"%>
<%@ include file="sangertableend.jsp"%>

	<%
			if (clients != null) {
			out.print("DAS - client legend:<br/>");
			for (int cl = 0; cl < clients.length; cl++) {
				DasClient client = clients[cl];
				String imgPath = client.getIconPath();
				String home = "" + client.getHomepage();
	%>
				<a href="<%=home%>"> 
				<%
 				if (imgPath != null) {
 				%><img src="<%=imgPath%>"></a>
					...
					<a href="<%=home%>"><%=client.getName()%></a>
					<br />
					<%
							}
							}
						}
					%>
<h3>Key:</h3>
<img src="img/red16.gif">Invalid (declared by provider to be used but not valid or required by the specification but not implemented)<br/>
<img src="img/amber16.gif">Probably valid (Registry detected a valid response but the provider has not specified capability)<br/>
<img src="img/green16.gif">Valid (Response is valid)<br/>
<img src="img/clear16.gif">Optional capability<br/>

</div>
</div>

<script src="boxover.js"></script>
<%@ include file="sangerfooter.jsp"%>






